package com.chzc.fm.duotin.adapter.viewpager;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.home.PlayList2Data;
import com.chzc.fm.duotin.ui.home.ClickableTextView;
import com.chzc.fm.duotin.utils.ImageUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/29.
 */
public class HomeMyPagerAdapter2 extends PagerAdapter {
    //需要填充的视图
    List<View> viewList = new ArrayList<>();
    //
    LayoutInflater inflater;
    //第一个页面显示的图片
    private ImageView imageView;
    private TextView tvTitle,tvSubTitle,tvContent;
    //第二个界面的文字内容
    private TextView tvContent2nd;

    private ClickableTextView clickableTextView ;

    public void setList(PlayList2Data list) {
        this.list = list;
    }

    PlayList2Data list;
    public HomeMyPagerAdapter2(final Context context, PlayList2Data list){
        inflater = LayoutInflater.from(context);
        this.list = list;
        //第一个界面
        View view1 = inflater.inflate(R.layout.vp_item,null);
        viewList.add(view1);
        imageView = (ImageView) view1.findViewById(R.id.ivConent);
        tvTitle = (TextView) view1.findViewById(R.id.tv_title);
        tvSubTitle = (TextView) view1.findViewById(R.id.tv_subTitle);
        tvContent = (TextView) view1.findViewById(R.id.tv_content);
        clickableTextView = (ClickableTextView) view1.findViewById(R.id.custom_df);
        clickableTextView.setItemOnClickListener(new ClickableTextView.ItemOnClickListener() {
            @Override
            public void onItemClick(int index) {
                if (index==0){
                    Toast.makeText(context, "点击了订阅事件", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(context, "点击了分享事件", Toast.LENGTH_SHORT).show();
                }
            }
        });
        //d第二个界面
        View view2 = inflater.inflate(R.layout.paget2,null);
        viewList.add(view2);
        tvContent2nd = (TextView) view2.findViewById(R.id.tv_contents);

    }
    @Override
    public int getCount() {
        return viewList==null?0:viewList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public void notifyDataSetChanged() {


        super.notifyDataSetChanged();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = viewList.get(position) ;

        if (position==0&&list!=null){
            //图片的url
            String url =list.getAlbum().getImage_url();
            //设置内容图片
            ImageLoader.getInstance().displayImage(url,imageView, ImageUtil.getDefaultOptions());
            //设置描述文字
            /*标题*/
            String title = list.getAlbum().getCategories().get(0)+"-"+list.getAlbum().getCategories().get(1);
            tvTitle.setText(title);
            /*副标题*/
            String subTitle ="节目 : "+list.getAlbum().getContent_num()+" 热度 : "+list.getAlbum().getPlay_num();
            tvSubTitle.setText(subTitle);
            /*上次更新*/
            String lastUpdateTime ="上次更新: "+list.getAlbum().getLast_updated_time();
            tvContent.setText(lastUpdateTime);
        }else if (position==1&&list!=null){

            //设置内容
            String conetent = list.getAlbum().getDescribe();
            tvContent2nd.setText(conetent);

        }

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }
}
